Page({
  data: {
    showsearch:false,   //显示搜索按钮
    searchtext:'',  //搜索文字
    filterdata:{},  //筛选条件数据
    showfilter:false, //是否显示下拉筛选
    showfilterindex:null, //显示哪个筛选类目
    sortindex:0,  //一级分类索引
    sortid:null,  //一级分类id
    subsortindex:0, //二级分类索引
    subsortid:null, //二级分类id
    cityindex:0,  //一级城市索引
    cityid:null,  //一级城市id
    subcityindex:0,  //二级城市索引
    subcityid:null, //二级城市id
    servicelist:[], //服务集市列表
    scrolltop:null, //滚动位置
    page: 0  //分页
  },
  onLoad: function () { //加载数据渲染页面
    this.fetchServiceData();
    this.fetchFilterData();
  },
  fetchFilterData:function(){ //获取筛选条件
    this.setData({
      filterdata:{
        "sort": [
            {
                "id": 0,
                "title": "全部"
            },
            {
              "id": 1,
              "title": "政策扶贫",
              "subsort": [
                {
                    "id": 1,
                    "title": "全部"
                },
                {
                    "id": 11,
                    "title": "精准扶贫"
                },
                {
                    "id": 12,
                    "title": "两项补贴"
                },
                {
                    "id": 13,
                    "title": "针对性帮扶"
                },
              ]
            },
            {
              "id": 2,
              "title": "企业扶贫",
              "subsort": [
                {
                    "id": 2,
                    "title": "全部"
                },
                {
                    "id": 21,
                    "title": "中国红十字基金会"
                },
                {
                    "id": 22,
                    "title": "希望工程"
                },
                {
                    "id": 23,
                    "title": "阿里巴巴"
                },
                {
                  "id": 24,
                  "title": "中国民生银行"
                },
              ]
            },
            {
              "id": 3,
              "title": "慈善项目",
              "subsort": [
                {
                    "id": 3,
                    "title": "全部"
                },
                {
                    "id": 31,
                    "title": "项目编号"
                },
                {
                    "id": 32,
                    "title": "项目名称"
                }
              ]
            },
            {
              "id": 4,
              "title": "网络扶贫",
              "subsort": [
                {
                    "id": 4,
                    "title": "全部"
                },
                {
                    "id": 41,
                    "title": "项目查询"
                },
                {
                    "id": 42,
                    "title": "受助对象查询"
                }
              ]
            },
        ],
        "city": [
              {
                "id": 0,
                "title": "未开始公示"
              },
             {
              "id": 1,
              "title": "公示中"},
             
             {
              "id": 2,
              "title": "公示结束",
               },
            ],
          }
       })
     },
  fetchServiceData:function(){  //获取城市列表
    let _this = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    const perpage = 10;
    this.setData({
      page:this.data.page+1
    })
    const page = this.data.page;
    const newlist = [];
    for (var i = 1; i <2; i++) {
      newlist.push({
        "id":1,
        "name":"江苏法律扶贫助力脱贫",
        "city":"未开始公示",
        "tag":"法律扶贫",
        "imgurl":"http://frjs.jschina.com.cn/31022/31027/201810/W020181019361064834178.png"
      },
       {
          "id":2,
         "name": "佛山对口凉山扶贫",
          "city": "公示中",
          "tag": "政策扶贫",
         "imgurl": "http://img.mp.itc.cn/upload/20170711/58c2f911da444167a22dd178b2e3da7f_th.jpg"
        },
        {
          "id": 3,
          "name": "四川广元贫困山区“菜单式”搬迁",
          "city": "公示结束",
          "tag": "政策扶贫",
          "imgurl": "http://paper.people.com.cn/rmrb/res/1/20180810/1533841253580_1.jpg"
        },
        {
          "id": 4,
          "name": "ME公益创新资助计划",
          "city": "公示结束",
          "tag": "网络扶贫",
          "imgurl": "http://www.xinhuanet.com//gongyi/2017-03/20/129513445_14899784686201n.jpg"
        },
        {
          "id": 5,
          "name": "“百千亿行动”耕地守护",
          "city": "未开始公示",
          "tag": "企业扶贫",
          "imgurl":"http://5b0988e595225.cdn.sohucs.com/images/20180923/8fecb024616e414fa52e6e0485c63660.jpeg"
        })
    }
    setTimeout(()=>{
      _this.setData({
        servicelist:_this.data.servicelist.concat(newlist)
      })
    },1500)
  },
  inputSearch:function(e){  //输入搜索文字
    this.setData({
      showsearch:e.detail.cursor>0,
      searchtext:e.detail.value
    })
  },
  submitSearch:function(){  //提交搜索
    console.log(this.data.searchtext);
    this.fetchServiceData();
  },
  setFilterPanel: function(e){ //展开筛选面板
    const d = this.data;
    const i = e.currentTarget.dataset.findex;
    if(d.showfilterindex == i){
      this.setData({
        showfilter: false,
        showfilterindex: null
      })
    }else{    
      this.setData({
        showfilter: true,
        showfilterindex:i,
      })
    }
    console.log(d.showfilterindex);
  },
  setSortIndex:function(e){ //服务类别一级索引
    const d= this.data;
    const dataset = e.currentTarget.dataset;
    this.setData({
      sortindex:dataset.sortindex,
      sortid:dataset.sortid,
      subsortindex: d.sortindex==dataset.sortindex ? d.subsortindex:0
    })
    console.log('服务类别id：一级--'+this.data.sortid+',二级--'+this.data.subsortid);
  },
  setSubsortIndex:function(e){ //服务类别二级索引
    const dataset = e.currentTarget.dataset;
    this.setData({
      subsortindex:dataset.subsortindex,
      subsortid:dataset.subsortid,
    })
    console.log('服务类别id：一级--'+this.data.sortid+',二级--'+this.data.subsortid);
  },
  setCityIndex:function(e){ //服务城市一级索引
    const d= this.data;
    const dataset = e.currentTarget.dataset;
    this.setData({
      cityindex:dataset.cityindex,
      cityid:dataset.cityid,
      subcityindex: d.cityindex==dataset.cityindex ? d.subcityindex:0
    })
    console.log('状态id：一级--'+this.data.cityid+',二级--'+this.data.subcityid);
  },
  setSubcityIndex:function(e){ //服务城市二级索引
    const dataset = e.currentTarget.dataset;
    this.setData({
      subcityindex:dataset.subcityindex,
      subcityid:dataset.subcityid,
    })
    console.log('状态id：一级--'+this.data.cityid+',二级--'+this.data.subcityid);
  },
  hideFilter: function(){ //关闭筛选面板
    this.setData({
      showfilter: false,
      showfilterindex: null
    })
  },
  scrollHandle:function(e){ //滚动事件
    this.setData({
      scrolltop:e.detail.scrollTop
    })
  },
  goToTop:function(){ //回到顶部
    this.setData({
      scrolltop:0
    })
  },
  //scrollLoading:function(){ //滚动加载
   // this.fetchServiceData();
 // },
  onPullDownRefresh:function(){ //下拉刷新
    this.setData({
      page:0,
      servicelist:[]
    })
    this.fetchServiceData();
    this.fetchFilterData();
    setTimeout(()=>{
      wx.stopPullDownRefresh()
    },1000)
  }
})